package com.yz.group5.med_booking.controller;

import com.yz.group5.med_booking.entity.Registration;
import com.yz.group5.med_booking.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    // 获取分页列表
    @GetMapping("/list")
    public Map<String, Object> getList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String keyword,
            Integer status) {
        return registrationService.getPageList(page, pageSize, keyword, status);
    }

    // 获取状态统计
    @PostMapping("/statusCounters")
    public Map<String, Integer> getStatusCounters() {
        return registrationService.getStatusCounters();
    }

    // 新增预约
    @PostMapping("/add")
    public Map<String, Object> addRegistration(@RequestBody Registration registration) {
        Map<String, Object> result = new HashMap<>();
        registration.setStatus(1); // 默认状态：预约中
        boolean success = registrationService.save(registration);
        result.put("code", success ? 200 : 500);
        result.put("message", success ? "添加成功" : "添加失败");
        return result;
    }

    // 开始就诊
    @PostMapping("/{id}/start")
    public Map<String, Object> startVisit(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        boolean success = registrationService.startVisit(id);
        result.put("code", success ? 200 : 500);
        result.put("message", success ? "开始就诊" : "操作失败");
        return result;
    }

    // 完成就诊
    @PostMapping("/{id}/complete")
    public Map<String, Object> completeVisit(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        boolean success = registrationService.completeVisit(id);
        result.put("code", success ? 200 : 500);
        result.put("message", success ? "就诊完成" : "操作失败");
        return result;
    }

    // 取消就诊
    @PostMapping("/{id}/cancel")
    public Map<String, Object> cancelVisit(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        boolean success = registrationService.cancelVisit(id);
        result.put("code", success ? 200 : 500);
        result.put("message", success ? "已取消" : "操作失败");
        return result;
    }

    // 标记爽约
    @PostMapping("/{id}/markNoShow")
    public Map<String, Object> markAsNoShow(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        boolean success = registrationService.markAsNoShow(id);
        result.put("code", success ? 200 : 500);
        result.put("message", success ? "已标记为爽约" : "操作失败");
        return result;
    }

    // 重新呼叫
    @PostMapping("/{id}/recall")
    public Map<String, Object> recallPatient(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        // 实际业务中可能需要调用语音播报等服务
        result.put("code", 200);
        result.put("message", "已重新呼叫");
        return result;
    }

    // 跳过患者
    @PostMapping("/{id}/skip")
    public Map<String, Object> skipPatient(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "已跳过");
        return result;
    }
}